<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search Function </title>
</head>

<body>
/**<br />
* <{$functionName}> ,<{$functionDescription}><br />
* <br />
* @param MySQLi $_dbObject  <br />
  <{foreach from=$whereStrs item=pa name=foo}>
* @param $<{$pa->valueName}><br />
  <{/foreach}>
  <{foreach from=$sortArray item=sortIndex name=foo}>
* @param $sortField<{$sortIndex}> ,column name which you use as sorting <br />
* @param $sortDirect<{$sortIndex}> ,0 means from min to max, 1 means from max to min <br />
  <{/foreach}>

* @return Array which contain the required data<br />
 */<br />


public function <{$functionName}>(MySQLi $_dbObject
  <{foreach from=$whereStrs item=pa name=foo}>
  ,
  $<{$pa->valueName}>
  <{/foreach}>
  <{foreach from=$sortArray item=sortIndex name=foo}>
  ,
  $sortField<{$sortIndex}>
  ,$sortDirect<{$sortIndex}>
  <{/foreach}>
){<br />
<br />
<br />

$whereStr=&quot; where 1=1 &quot;;<br />
  <br />
  <{foreach from=$whereStrs item=wStr name=foo}>
   if(isset($<{$wStr->valueName}>) <{if $wStr->singleQuotes=="'"}> &amp;&amp; $<{$wStr->valueName}>!=&quot;&quot;<{/if}>){<br />
$whereStr.=sprintf(&quot; and `<{$wStr->fieldName}>` <{$wStr->operator}> 
   <{$wStr->singleQuotes}><{$wStr->likeHeadStr}><{$wStr->sprintfStr}><{$wStr->likeTailStr}><{$wStr->singleQuotes}>&quot;,
   <{if $wStr->escapeStr==1}>$this-&gt;escape_string(<{/if}>$<{$wStr->valueName}><{if $wStr->escapeStr==1}>)<{/if}>);<br />
<br />
<br />}<br />
<br />
 <{/foreach}>
<br />

$orderbySt=&quot;&quot;;


<{foreach from=$sortArray item=sortIndex name=foo}>
<{if $sortIndex==1}>$orderbyFlag=0;<br /><{/if}>
if($sortField<{$sortIndex}>!=null&&$sortField<{$sortIndex}>!="")
{<br /> 

$orderDirec=&quot;&quot;;<br />
if(intval($sortDirect<{$sortIndex}>)==1){<br />
<br />
$orderDirec=&quot; desc &quot;;
<br />
<br />}<br />
if($orderbyFlag==0){<br />
<br />
$orderbySt=&quot; order by `&quot;.$sortField<{$sortIndex}>.'` '.$orderDirec;
<br />
<br />}<br />
else{<br />
<br />
$orderbySt.=&quot; ,  `&quot;.$sortField<{$sortIndex}>.'` '.$orderDirec; 
<br />
<br />}<br />
$orderbyFlag++;<br />
<br />
<br />}<br />
  <br />
  <{/foreach}>
<br />
  
  $sql=&quot;select 
  <{foreach from=$attributes item=col name=foo}>
  <{if ($smarty.foreach.foo.index !=0)}>,<{/if}><{$col->Field}> <{/foreach}>
  <{$joinSql}> <{$tableName}> &quot;.$whereStr.$orderbySt;<br />

//echo $sql.&quot;&lt;br //&gt;&quot;;<br />
  <br />
<br />
<br />
			$result=$_dbObject-&gt;query($sql);<br />
			<br />
			$re = array();<br />
			<br />
			if($result !==false){<br />
			<br />
				if($result-&gt;num_rows&gt;0){<br />
					$row =$result-&gt;fetch_row();<br />
			<br />
			<br />
					while ($row!==NULL){<br />
						$one=new <{$className}>;<br />
						<br />
						<{foreach from=$attributes item=field name=foo}>
						$one-&gt;<{$field->attribute}>=$row[<{$smarty.foreach.foo.index}>];<br />
						<{/foreach}>
						$re[]=$one;<br />
			<br />
						$row =$result-&gt;fetch_row();<br />
						<br />
					}<br />
				}<br />
				$result-&gt;free();<br />
			<br />
			}<br />
			<br />
			<br />
<br />
<br />
			return $re;




<br />
<br />}<br />

<{if $createPageFunction}>
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<br />

<br />
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<br />
/**<br />
* Paged<{$functionName}> ,Paged <{$functionDescription}><br />
* <br />
* @param MySQLi $_dbObject  <br />
  <{foreach from=$whereStrs item=pa name=foo}>
* @param $pageNumber , how many items in one page <br />
* @param $pageSize, the page number you want to fetch <br />
* @param $<{$pa->valueName}><br />
  <{/foreach}>
  <{foreach from=$sortArray item=sortIndex name=foo}>
* @param $sortField<{$sortIndex}> ,column name which you use as sorting <br />
* @param $sortDirect<{$sortIndex}> ,0 means from min to max, 1 means from max to min <br />
  <{/foreach}>

* @return Array which contain the required data<br />
 */<br />


 public  function Paged<{$functionName}>(MySQLi $_dbObject,$pageNumber,$pageSize
<{foreach from=$whereStrs item=pa name=foo}>
,
$<{$pa->valueName}>
<{/foreach}>
<{foreach from=$sortArray item=sortIndex name=foo}>
,$sortField<{$sortIndex}>
,$sortDirect<{$sortIndex}>
<{/foreach}>
){<br />


<br />

$whereStr=&quot; where 1=1 &quot;;<br />
  <br />
  <{foreach from=$whereStrs item=wStr name=foo}>
   if(isset($<{$wStr->valueName}>) <{if $wStr->singleQuotes=="'"}> &amp;&amp; $<{$wStr->valueName}>!=&quot;&quot;<{/if}>){<br />
$whereStr.=sprintf(&quot; and `<{$wStr->fieldName}>` <{$wStr->operator}> 
   <{$wStr->singleQuotes}><{$wStr->likeHeadStr}><{$wStr->sprintfStr}><{$wStr->likeTailStr}><{$wStr->singleQuotes}>&quot;,
   <{if $wStr->escapeStr==1}>$this-&gt;escape_string(<{/if}>$<{$wStr->valueName}><{if $wStr->escapeStr==1}>)<{/if}>);<br />
<br />
<br />}<br />
<br />
 <{/foreach}>
<br />

$sql=&quot;select 
count(*) 
   <{$joinSql}> &quot;.$whereStr;<br />



			$result=$_dbObject-&gt;query($sql);<br />
<br />
			$_error=true;<br />
			if($result !==false){<br />
<br />
				if($result-&gt;num_rows&gt;0){<br />
					$row =$result-&gt;fetch_row();<br />
<br />
<br />
					if ($row!==NULL){<br />
<br />
<br />
						$_error=false;<br />
						$totalRows=$row[0];<br />
							<br />
					}<br />
				}<br />
			}<br />
				<br />
			if($_error){<br />
				return FALSE;<br />
			}<br />


			$result-&gt;free();<br />


			$re=array();<br />
			<br />
			$re['totalRows'] =$totalRows;<br />
<br />
			
			$re['pageSize']=intval($pageSize);<br />
<br />
			if($re['pageSize']==0){<br />
<br />
				$re['pageSize']=10;<br />
<br />
<br />
			}<br />
			$re['totalPages']=intval($re['totalRows']/$re['pageSize']);<br />
<br />
			if ($re['totalRows']%$re['pageSize']!=0){<br />
				$re['totalPages']+=1;<br />
<br />
<br />
			}<br />
			$re['pageNumber']=intval($pageNumber);<br />
<br />
			if($re['pageNumber']&lt;1) {<br />
<br />
				$re['pageNumber']=1;<br />
<br />
			}<br />
			if($re['pageNumber']&gt;$re['totalPages']){<br />
<br />
				$re['pageNumber']=$re['totalPages'];<br />
<br />
			}<br />
<br />
			if($re['totalRows'] ==0){<br />
				return $re;<br />
			}<br />
			$offset=$re['pageSize']*($re['pageNumber'] - 1);<br />




<br />
$orderbySt=&quot;&quot;;


<{foreach from=$sortArray item=sortIndex name=foo}>
<{if $sortIndex==1}>$orderbyFlag=0;<br /><{/if}>
if($sortField<{$sortIndex}>!=null&&$sortField<{$sortIndex}>!="")
{<br /> 

$orderDirec=&quot;&quot;;<br />
if(intval($sortDirect<{$sortIndex}>)==1){<br />
<br />
$orderDirec=&quot; desc &quot;;
<br />
<br />}<br />
if($orderbyFlag==0){<br />
<br />
$orderbySt=&quot; order by `&quot;.$sortField<{$sortIndex}>.'` '.$orderDirec;
<br />
<br />}<br />
else{<br />
<br />
$orderbySt.=&quot; ,  `&quot;.$sortField<{$sortIndex}>.'` '.$orderDirec; 
<br />
<br />}<br />
$orderbyFlag++;<br />
<br />
<br />}<br />
  <br />
  <{/foreach}>
<br />
  
  

$sql=&quot;select 
<{foreach from=$attributes item=col name=foo}>
<{if ($smarty.foreach.foo.index !=0)}>,<{/if}>`<{$col->Field}>` 
 <{/foreach}>
  <{$joinSql}> &quot;.$whereStr .$orderbySt.&quot; limit &quot;.$offset.&quot;,&quot;.$re['pageSize'];<br />

  
//echo $sql.&quot;&lt;br //&gt;&quot;;<br />
  <br />
<br />
<br />
			$result=$_dbObject-&gt;query($sql);<br />
			<br />
			$re['data'] = array();<br />
			$data=&amp;$re['data'];<br />
<br />
			<br />
			if($result !==false){<br />
			<br />
				if($result-&gt;num_rows&gt;0){<br />
					$row =$result-&gt;fetch_row();<br />
			<br />
			<br />
					while ($row!==NULL){<br />
						$one=new <{$className}>;<br />
						<br />
						<{foreach from=$attributes item=field name=foo}>
						$one-&gt;<{$field->attribute}>=$row[<{$smarty.foreach.foo.index}>];<br />
						<{/foreach}>
						$data[]=$one;<br />
			<br />
						$row =$result-&gt;fetch_row();<br />
						<br />
					}<br />
				}<br />
				$result-&gt;free();<br />
			<br />
			}<br />
			<br />
			<br />
<br />
<br />
			return $re;

<br />
<br />}<br />
  <{/if}>



<br />
<br /></body>
</html>
